﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WLMCoreLibrary;
using MiWebSite_Core;
using System.Text;


namespace MWS_RealSate
{
    public partial class _Default : System.Web.UI.Page
    {
        WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
        dbConnector ConexionDB;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.QueryString["xml"] != null)
            {
                GetPageXML();
            }
            else
                TransformXML(GeneratePage());

        }

        private MiWebSitePage GeneratePage()
        {
            ConexionDB = new dbConnector(config.Connections[0].value);
            MiWebSitePage pagina = new MiWebSitePage();
            try
            {
                pagina = new MiWebSitePage(ref ConexionDB);                
            }
            catch (Exception ex)
            {

            }
            ConexionDB.CloseConnection();
            return pagina;
        }

        private void TransformXML(MiWebSitePage pagina)
        {
            List<ParametroTransformacionXML> parametros = new List<ParametroTransformacionXML>();
            parametros.Add(new ParametroTransformacionXML("isMobile", MiWebSiteFunctions.isMobileBrowser().ToString().ToLower()));
            parametros.Add(new ParametroTransformacionXML("skinPath", config.Settings["skinPath"].value));
            string HTMLPagina = WLMFunctions.TransformXML(pagina, config.Settings["siteURL"].value + config.Settings["skinPath"].value + "index.xsl", parametros);
            Response.Write(HTMLPagina);
            Response.End();
        }

        private void GetPageXML()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.HeaderEncoding = Encoding.UTF8;
            string xmlportada = WLMFunctions.SerializeObject(GeneratePage(), true);
            Response.Write(xmlportada);
            Response.End();


        }
    }
}